#include "CControlManage.h"
#include "CControlTool.h"
#include "QUdpServer.h"

NETWORKSERVICE_BEGIN_NAMESPACE

std::map<string, cmdHandleBase*> cmdHandleBase::m_HanderMap;
QUdpServer* cmdHandleBase::m_UdpService = nullptr;
// http服务器初始化
CEntranceHttpServer cmdHandleBase::m_HttpServer;
// http客户端
CEntranceHttpClient cmdHandleBase::m_HttpClient;
// mqtt客户端
CEntranceMqttClient cmdHandleBase::m_MqttClient;



// 获取字符串md5
bool controlManage::runUdpHandle(T_CmdBaseData *head, string &data, tRoute *route)
{
    return cmdHandleBase::runUdpHandle(head, data, route);
}


// 本设备作为http服务端收到消息
bool controlManage::runHttpHandle(struct _tCmdBaseData *head, string &body, struct tRoute *route)
{
    return cmdHandleBase::runHttpHandle(head, body, route);
}

// 本设备收到消息mqtt
bool controlManage::runMqttHandle(struct _tCmdBaseData *head, string &body, struct tRoute *route)
{
    return cmdHandleBase::runMqttHandle(head, body, route);
}

string controlManage::strMd5(const string &str)
{
    return CControlTool::strMd5(str);
}

string controlManage::getSignature(const string &body)
{
    return CControlTool::getSignature(body);
}



// 获取设备sn
string controlManage::getDevSn()
{
    return CControlTool::getDevSn();
}


// 初始化服务端
bool controlManage::initServer(int32_t httpServerPort, int32_t udpPort)
{
    bool ret = false;
    if(udpPort > 0){
        if(!cmdHandleBase::m_UdpService){
            cmdHandleBase::m_UdpService = new QUdpServer(nullptr);
        }
        ret = cmdHandleBase::m_UdpService->start(udpPort);
    }
    if(httpServerPort > 0){
        string strIp = string("http://0.0.0.0:") + to_string(CNetConfig::getConfig().httpServicePort);
        cmdHandleBase::m_HttpServer.startServer(strIp);
    }
    return ret;
}

NETWORKSERVICE_END_NAMESPACE
